$(document).ready(function(){
    loading();    
    var payId="";
});

// 动态添加页面
function loading(){
    $(".payEvaluate_main").find(".hotel").remove();
    str="";
    var content_url = window.location.href;
    content_url = content_url.split("?");
    payAmount = content_url[ content_url.length-1 ].split("=")[ content_url[ content_url.length-1 ].split("=").length-1 ];
    console.log(payAmount);
    roomName=content_url[ content_url.length-2 ].split("=")[ content_url[ content_url.length-2 ].split("=").length-1 ];
    roomName = decodeURI( roomName );
    console.log(roomName);
    hotelName=content_url[ content_url.length-3 ].split("=")[ content_url[ content_url.length-3 ].split("=").length-1 ];
    hotelName = decodeURI( hotelName );
    console.log(hotelName);
    hotelPicture=content_url[ content_url.length-4 ].split("=")[ content_url[ content_url.length-4 ].split("=").length-1 ];
    console.log(hotelPicture);
    payId=content_url[ content_url.length-5 ].split("=")[ content_url[ content_url.length-5].split("=").length-1 ];
    console.log(payId);
    str+=`
    <div class="hotel clearfix">
        <img src="`+url_file+hotelPicture+`" class="hotel_pic lf">
        <div class="hotel_msg lf">
            <div class="hotel_name">
                    `+hotelName+`
            </div>
            <div class="room_name">
                    `+roomName+`
            </div>
        </div>
        <div class="money gt">
            ￥<span>`+payAmount+`</span>
        </div>
    </div>
    `;
    $(".payEvaluate_main").prepend(str);
}
// star
var star=""
$(".star1").click(function(){
    $(".starNum").find("i").css("background","url(img/icon_star_no.png) no-repeat");
    $(this).css("background","url(img/icon_star_yes.png) no-repeat");
    star=1;
})
$(".star2").click(function(){
    $(".starNum").find("i").css("background","url(img/icon_star_no.png) no-repeat");
    $(".star1").css("background","url(img/icon_star_yes.png) no-repeat");
    $(this).css("background","url(img/icon_star_yes.png) no-repeat");
    star=2;
})
$(".star3").click(function(){
    $(".starNum").find("i").css("background","url(img/icon_star_no.png) no-repeat");
    $(".star1").css("background","url(img/icon_star_yes.png) no-repeat");
    $(".star2").css("background","url(img/icon_star_yes.png) no-repeat");
    $(this).css("background","url(img/icon_star_yes.png) no-repeat");
    star=3
})
$(".star4").click(function(){
    $(".starNum").find("i").css("background","url(img/icon_star_no.png) no-repeat");
    $(".star1").css("background","url(img/icon_star_yes.png) no-repeat");
    $(".star2").css("background","url(img/icon_star_yes.png) no-repeat");
    $(".star3").css("background","url(img/icon_star_yes.png) no-repeat");
    $(this).css("background","url(img/icon_star_yes.png) no-repeat");
    star=4;
})
$(".star5").click(function(){
    $(".starNum").find("i").css("background","url(img/icon_star_yes.png) no-repeat");
    star=5;
})


// 上传图片
var Readder1="";
$(".uploading_img").on("change",'#comment_img',function () {
    $(this).find("#preview").css({"width":"58px","height":"58px","margin":"0"});
    var previewImg = document.getElementById('preview');
    var file = this.files[0];    
    // 获取文件读取对象
    str="<img id='preview'>"
    $(this).parent().append();
    var reader = new FileReader();
    // 文件读取完后的展示图片
    reader.addEventListener('load', function () {
        if(file.size>3*1024*1024){
            alert("上传图片大小不得超过3M");
        }else{
            previewImg.src = reader.result;
            Readder1=file;
            // dataURItoBlob(Readder1);
            console.log(Readder1);
        }
    }, false);
    reader.readAsDataURL(file);
    $(".close").css("display","block");
    strDiv="";
    console.log($(".uploading_img").find(".lable").length);
    $(this).attr("id","");
    $("#preview").attr("id","");
    if($(".uploading_img").find(".lable").length<10){
        strDiv=`
                <a class="label" id="label1">
                    <input type="file" id="comment_img">
                    <img id="preview" src="img/icon_photo.png">
                    <div class="close">x</div>
                </a>
               `
        $(".uploading_img span").before(strDiv);
    }
})
$(".uploading_img").on("click",'.close',function(){
    // event.stopPropagation();
    console.log(1)
    $(this).parent().remove();
  
})

// 提交
var evaluate_judge = 1;
$(".submitBtn").click(function(){
    var token=localStorage.token;
    var content=$(".uploading textarea").val();
    var img_url=[];
    var picturePathArry=""
    var file_img =$("#formList .uploading_img input");
    if(file_img.length >1 ){
        for(i=0;i<file_img.length-1;i++){
            picturePathArry = file_img[i].files[0];
            var abc = new FormData();
            abc.append( 'token',token );
            abc.append( 'type',"evaluate" );
            abc.append( 'img',picturePathArry );
            $.ajax({
                url:url+"v1/upload/picture",
                dataType:"json",                
                async: true,
                method:"POST",
                data:abc,                
                processData: false,
                contentType: false,
                xhrFields: {
                    withCredentials: true // 允许跨域名储存和访问cookie
                }
            }).done(function(data){
                console.log( data );
                if( data.code == "success" ){
                    console.log( data.data.picturePath );                    
                    img_url.push( data.data.picturePath );
                    console.log(img_url);
                    evaluate_judge = 1;
                    return;
                }
                if( data.code == "account_token_invalid" ){
                    evaluate_judge = 2;
                    alert( '身份已失效,请重新登陆' );
                    return;
                }
                if( data.code == "image_create_fail" ){
                    evaluate_judge = 2; evaluate_judge = 2;
                    alert( '图片上传失败,请重新上传' );
                    return;
                }
            })
        }
    }
    
    if( star==""){
        alert("请选取评分等级");
    }
    if(content==""){
        alert("请填写评价内容")
    }
    setTimeout(function(){
        if( evaluate_judge == 1 ){
            img_url = JSON.stringify( img_url );
            var content = $(".uploading textarea").val();
            console.log(img_url);
            $.ajax({
                method: "POST",
                url: url + "v1/order/evaluate",
                async: true,
                dataType: "json",
                data:{
                    token:token,
                    orderId:payId,
                    score:star,
                    content:content,
                    picturePathArry:img_url
                },
                xhrFields: {
                    withCredentials: true
                }
            })
            .done(function(data){
                console.log(data.message);
                if( data.code == "success" ){
                alert( '评价成功' );
                //    window.location.href="payList.html";
                    return;
                }
                if( data.code == "account_token_invalid" ){
                    alert( '身份已失效,请重新登陆' );
                    return;
                }
                if( data.code == "already_evaluated" ){
                    alert( '该订单已经评价过了' );
                    // window.location.href="payList.html";
                    return;
                }
                if( data.code == "evaluate_fail" ){
                    alert('评价失败' );
                    return;
                }
            })
        
        console.log( img_url );
          
        }        
    },1000)
})
